package com.ezia.coreservices.charging.utils;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Map;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.digest.HmacUtils;
import org.springframework.stereotype.Component;

import net.sf.json.JSONObject;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

/**
 * AES加解密算法
 * 
 * @author yangcheng
 */

@Component
public class AesEncryptionUtil {

	// 加密
		public static String Encrypt(String sSrc, String dataSecret, String dataSecretIv) throws Exception {
			if (dataSecret == null) {
				System.out.print("dataSecret为空null");
				return null;
			}
			// 判断dataSecret是否为16位
			if (dataSecret.length() != 16) {
				System.out.print("dataSecret长度不是16位");
				return null;
			}
			byte[] raw = dataSecret.getBytes();
			SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
			Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");// "算法/模式/补码方式"
			IvParameterSpec iv = new IvParameterSpec(dataSecretIv.getBytes());// 使用CBC模式，需要一个向量iv，可增加加密算法的强度
			cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
			byte[] encrypted = cipher.doFinal(sSrc.getBytes());

			return new BASE64Encoder().encode(encrypted).replaceAll("\\r\\n", "");// 此处使用BASE64做转码功能，同时能起到2次加密的作用。
		}

		// 解密
		public static String Decrypt(String sSrc, String dataSecret, String dataSecretIv) throws Exception {
			try {
				// 判断dataSecret是否正确
				if (dataSecret == null) {
					System.out.print("dataSecret为空null");
					return null;
				}
				// 判断dataSecret是否为16位
				if (dataSecret.length() != 16) {
					System.out.print("dataSecret长度不是16位");
					return null;
				}
				byte[] raw = dataSecret.getBytes("ASCII");
				SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
				Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
				IvParameterSpec iv = new IvParameterSpec(dataSecretIv.getBytes());
				cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
				byte[] encrypted1 = new BASE64Decoder().decodeBuffer(sSrc);// 先用base64解密
				try {
					byte[] original = cipher.doFinal(encrypted1);
					String originalString = new String(original);
					return originalString;
				} catch (Exception e) {
					System.out.println(e.toString());
					return null;
				}
			} catch (Exception ex) {
				System.out.println(ex.toString());
				return null;
			}
		}
		
		/**
		 * 加密传输数据
		 * 
		 * @param
		 * @return 加密后的字符串
		 */
		public String encryptData(Map<String, Object> mEncryptData,String DataSecret,String DataSecretIV) throws Exception {
			return AesEncryptionUtil.Encrypt(JSONObject.fromObject(mEncryptData).toString(), DataSecret, DataSecretIV);
		}

		/**
		 * 解密传输数据
		 * 
		 * @param
		 * @return 解密后的字符串
		 */
		public String decryptData(String data,String DataSecret,String DataSecretIV) throws Exception {
			// 解密返回数据
			String deString = AesEncryptionUtil.Decrypt(data, DataSecret, DataSecretIV);
			return deString;
		}
		
		/**
		 * 获取签名
		 * 
		 * @param data      加密后的传输数据
		 * @param timeStamp 时间戳字符串
		 * @return 
		 */
		public String getSign(String data, String timeStamp,String SigSecret,String OperatorId,String SEQ) {
			String signData = HmacUtils.hmacMd5Hex(SigSecret, OperatorId + data + timeStamp + SEQ).toUpperCase();
			return signData;
		}

		/**
		 * 获取时间戳字符串123
		 * 
		 * @param
		 * @return
		 */
		public String getTimestamp() {
			SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
			Timestamp d = new Timestamp(System.currentTimeMillis());
			return df.format(d);
		}
		
}
